package com.mybatis.dao.impl;

import com.mybatis.bean.Team;
import com.mybatis.dao.TeamDao;
import com.mybatis.utils.MySqlSession;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class TeamDaoImpl implements TeamDao {
    @Override
    public List<Team> queryAll() {
        SqlSession sqlSession = MySqlSession.createSqlSession();
        List<Team> teams = sqlSession.selectList("com.mybatis.bean.Team.queryAll");
        for (Team team :
                teams) {
            System.out.println(team.toString());
        }
        sqlSession.close();
        return teams;
    }

    @Override
    public Team queryById(Integer teamId) {
        SqlSession sqlSession = MySqlSession.createSqlSession();
        Team selectOne = sqlSession.selectOne("com.mybatis.bean.Team.findById", teamId);
        sqlSession.close();
        return selectOne;
    }

    @Override
    public int add(Team team) {
        SqlSession sqlSession = MySqlSession.createSqlSession();
        int one = sqlSession.insert("com.mybatis.bean.Team.insertTeam", team);
        if (one >0) {
            sqlSession.commit();
        }
        sqlSession.close();
        return one;
    }

    @Override
    public int update(Team team) {
        SqlSession sqlSession = MySqlSession.createSqlSession();
        int one = sqlSession.update("com.mybatis.bean.Team.updateTeam", team);
        if (one >0) {
            sqlSession.commit();
        }
        sqlSession.close();
        return one;
    }

    @Override
    public int del(Integer teamId) {
        SqlSession sqlSession = MySqlSession.createSqlSession();
        int one = sqlSession.delete("com.mybatis.bean.Team.deleteTeam", teamId);
        if (one >0) {
            sqlSession.commit();
        }
        sqlSession.close();
        return one;
    }
}
